﻿# !/usr/bin/env python
#-*- coding:utf-8 -*-
import requests
import re
import os
import sys
j=0
def download_img(img_url,path):
    try:
        response=requests.get(img_url)
        print("正在下载第{}张图片...".format(j))
        fb=open(path,'wb')
        fb.write(response.content)
        fb.close()
    except:
        print("出现错误跳过执行")
cid=input('请输入数字：大胸2，翘臀6，黑丝7，美腿3，颜值4，大杂烩5：')
for num in range(1,1000):
    html_url='https://www.dbmeinv.com/dbgroup/show.htm?cid={}&pager_offset={}'.format(cid,num)
    print("爬取第{}个页面...".format(num))
    response = requests.get(html_url)
    if response.status_code==404:
        print("第{}个页面不存在".format(num))
        continue
    html = (response.text)
    page_url_info = re.findall(r'a href="(.*?)" class="link" target="', html)
    t=0
    for page_url in page_url_info:
        response = requests.get(page_url)
        if response.status_code != 200:
            print("加载错误")
            continue
        t=t+1
        print("爬取第{}个页面中的第{}个帖子".format(num,t))
        html = (response.text)
        user_id = re.findall(r'a data-author="true" data-name="(.*?)"', html)
        for user in user_id:
            if user!='':
                break
        try:
            title=re.findall(r'class="media-heading">(.*?)<', html)[0]
        except:
            title='1'
        img_url_info = re.findall(r'img src="(.*.jpg)"', html)
        i=0
        for img_url in img_url_info:
            i=i+1
            xldvpath='%s.%s.%s.jpg'%(title,user,i)
            jdpath=os.path.join(sys.path[0],xldvpath)
            j=j+1
            print (xldvpath)
            download_img(img_url, jdpath)

